package com.shaifish.common_sys.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaifish.common_sys.utils.CurrentUserUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;

/**
 * Base业务实现类
 *
 * @author ShaiFish
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements IBaseService<T> {

    /**
     * 数据保存前逻辑
     */
    protected void saveBefore(T t) {
    }

    /**
     * 数据保存后逻辑
     */
    protected void saveAfter(T t) {
    }

    /**
     * 数据更新前逻辑
     */
    protected void updateBefore(T t) {
    }

    /**
     * 数据更新后逻辑
     */
    protected void updateAfter(T t) {
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(T entity) {
        saveBefore(entity);
        resolveEntity(entity);
        boolean result = super.save(entity);
        saveAfter(entity);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(T entity) {
        updateBefore(entity);
        this.resolveEntity(entity);
        boolean result = super.updateById(entity);
        updateAfter(entity);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatchById(Collection<T> entityList) {
        entityList.forEach(this::resolveEntity);
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
        return StringUtils.isEmpty(entity.getId()) ? this.save(entity) : this.updateById(entity);
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveOrUpdateBatch(entityList, batchSize);
    }

    @Override
    public boolean checkIdExist(String id) {
        return lambdaQuery().eq(T::getId, id).count() > 0;
    }

    /**
     * 插入通用数据
     *
     * @param entity
     */
    @SneakyThrows
    private void resolveEntity(T entity) {
        // 1.0版本 从session中拿取当前用户
        Long currentUser = CurrentUserUtil.getCurrentUser();
        Date date = new Date();
        if (StringUtils.isEmpty(entity.getId())) {
            if (currentUser != null) {
                entity.setCreateUser(currentUser);
                entity.setUpdateUser(currentUser);
                // 如果后续有部门还可以添加部门
            }
            entity.setCreateTime(date);
        } else if (currentUser != null) {
            entity.setUpdateUser(currentUser);
        }

        entity.setUpdateTime(date);
    }

}
